
public  anal_temper

ss7      segment     code
rseg    ss7

extrn   code(write_com)    ;;;
extrn   code(write_data)   ;;;
extrn   code(put_str)

cxpos   equ     21h
cypos   equ     22h

temper_l    equ     3bh
temper_h    equ     3ah
temper_d    equ     3ch  
temper_num  equ     4ah

flag1       bit     20h.1
dq          bit     p2.2

bai_c       equ     3dh
sh_c        equ     3eh
g_c         equ     3fh





anal_temper:
    push    0
    mov     a,#01h
    lcall   write_com
   

    mov     cxpos,#00h
    mov     cypos,#00h
    mov     dptr,#table5
    lcall    put_str
    
    lcall get_temper
    lcall temper_cov
    mov a,temper_num            ;divide temper_num
    anl a,#0f0h
    swap a
    add     a,#30h
    mov     bai_C,a
    lcall   write_data
   
    mov a,temper_num
    anl a,#0fh
    add     a,#30h
    mov     sh_c,a
    lcall   write_data
   
    mov     a,#2eh             ;2e='.'
    lcall   write_data

    mov     a,temper_d
    add     a,#30h
    mov     g_c,a
    lcall   write_data

    mov     a,#43h             ;43h='C'
    lcall   write_data

   


        
    pop     0
    ret

;------------------read transfered tempr value
get_temper: 
    lcall init_1820    ;init program
    setb dq ; timer entry
    bcd:
    jb flag1,s22
    ljmp bcd ; if ds18b20 not exist than return

s22:
    lcall delay1
    mov a,#0cch ; skip rom matching------0cc
    lcall write_1820
    mov a,#44h ; send tempr transferring command
    lcall write_1820
    nop
    lcall delay
    lcall delay
cba:
    lcall init_1820
    jb flag1,abc
    ljmp cba
abc:
    lcall delay1
    mov a,#0cch ; skip rom matching
    lcall write_1820
    mov a,#0beh ; send tempr reading command
    lcall write_1820
    lcall read_18200 ;or read_1820
    ret

;------------------read ds18b20, read 1 byte from ds18b20
read_1820:
    mov r2,#8
    re1:
    clr c
    setb dq
    nop
    nop
    clr dq
    nop
    nop
    nop
    setb dq
    mov r3,#7
    djnz r3,$
    mov c,dq
    mov r3,#23
    djnz r3,$
    rrc a
    djnz r2,re1
    ret

;-------------------write ds18b20 program
write_1820:
    mov r2,#8
    clr c
    wr1:
    clr dq
    mov r3,#6
    djnz r3,$
    rrc a
    mov dq,c
    mov r3,#23
    djnz r3,$
    setb dq
    nop
    djnz r2,wr1
    setb dq
    ret
;-------------------read ds18b20 program
read_18200:
    mov r4,#2 ; get low and high bit of tempr from ds18b20
    mov r1,#3bh ; low to 36h(temper_l),hight to 35h(temper_h)
    re00:
    mov r2,#8
    re01:
    clr c
    setb dq
    nop
    nop
    clr dq
    nop
    nop
    nop
    setb dq
    mov r3,#7
    djnz r3,$
    mov c,dq
    mov r3,#23
    djnz r3,$
    rrc a
    djnz r2,re01
    mov @r1,a
    dec r1
    djnz r4,re00
    ret

;-------------------Read from ds18b20 and do translation
temper_cov:

    mov a,#0f0h
    anl a,temper_l ; give up the 4 digital values after point.
    swap a
    mov temper_num,a
    mov a,temper_l
    jnb acc.3,temper_cov1 ; get tempr value (45)
    inc temper_num

temper_cov1:
    mov a,temper_h
    anl a,#07h
    swap a
    orl a,temper_num
    mov temper_num,a ; save tempr data after transferring
    mov a,#0fh
    anl a,temper_l
    
    mov temper_d,a
    clr c
    subb a,#10
    jc jianlo
    mov temper_d,a
    inc temper_num

jianlo:
    lcall bin_bcd
    ret
;-------------------Hex 16 tempr data --> bcd code
bin_bcd:
    mov dptr,#temp_tab
    mov a,temper_num
    movc a,@a+dptr
    mov temper_num,a   
    ret


;-------------------ds18b20 init program
init_1820:
    setb dq
    nop
    clr dq
    mov r0,#80h
tsr1:
    djnz r0,tsr1 ; delay
    setb dq
    mov r0,#25h ;96us-25h
tsr2:
    djnz r0,tsr2
    jnb dq,tsr3
    ljmp tsr4 ; delay
tsr3:
    setb flag1 ; set flag means ds1820 exist
    ljmp tsr5
tsr4:
    clr flag1 ; clear flag means ds1820 not exist
    ljmp tsr7
tsr5:
    mov r0,#06bh ;200us
tsr6:
    djnz r0,tsr6 ; 
tsr7:
    setb dq
    ret
;------------------rewrite ds18b20 tempro register
re_config:
    jb flag1,re_config1 ; if ds18b20 exist ,then switch to re_config1
    ret
re_config1:
    mov a,#0cch ; send skip rom command
    lcall write_1820
    mov a,#4eh ; send write to tempro register command
    lcall write_1820
    
    mov a,#00h ; th(top limit of alram) 00h
    lcall write_1820
    mov a,#00h ; tl(low limit of alram) 00h
    lcall write_1820
    mov a,#7fh ; select 12 bits tempr analysis mode
    lcall write_1820
    ret
;------------------delay program
delay:
    mov r7,#2h
min:
    djnz r7,ys500
    ret
ys500:
    lcall ys500us
    ljmp min
ys500us:
    mov r6,#200
    djnz r6,$
    ret
delay1:
    mov r7,#20h
    djnz r7,$
    ret



temp_tab:
db 00h,01h,02h,03h,04h,05h,06h,07h
db 08h,09h,10h,11h,12h,13h,14h,15h
db 16h,17h,18h,19h,20h,21h,22h,23h
db 24h,25h,26h,27h,28h,29h,30h,31h
db 32h,33h,34h,35h,36h,37h,38h,39h
db 40h,41h,42h,43h,44h,45h,46h,47h
db 48h,49h,50h,51h,52h,53h,54h,55h
db 56h,57h,58h,59h,60h,61h,62h,63h
db 64h,65h,66h,67h,68h,69h,70h,71h
db 72h,73h,74h,75h,76h,77h,78h,79h
db 80h,81h,82h,83h,84h,85h,86h,87h
db 88h,89h,90h,91h,92h,93h,94h,95h
db 96h,97h,98h,99h

table5:   db      "Local T:",00h


end